<?php

	function getConfigDbParams()
	{
		$curDir = getcwd();
		$params = array();
		$rawConfigContents = file("$curDir/application/config.ini");
		foreach( $rawConfigContents as $row )
		{
			list($kwd,$val) = explode("=", $row);
			if(trim($kwd)=="db.params.host")
			{
				$params["host"] = trim($val);
			}
			else
			if(trim($kwd)=="db.params.username")
			{
				$params["username"] = trim($val);
			}
			else
			if(trim($kwd)=="db.params.password")
			{
				$params["password"] = trim($val);
			}
			else
			if(trim($kwd)=="db.params.dbname")
			{
				$params["dbname"] = trim($val);
			}
		}
		return $params;
	}

	function getCaseMerchandise( $p, $caseNum )
	{
		$query = "Select * From case_merchandise Where FDMS_case_num ='$caseNum'";
		$results = array();
		$pdo_str = "mysql:";
		$pdo_str .= "host=".$p['host'].";";
		$pdo_str .= "dbname=".$p['dbname'];
		
		try
		{
			$results['status'] = "OK";
			$results['rows'] = array();
			
			$items = array();
			
			$pdo = new PDO($pdo_str, $p['username'], $p['password']);
			$rows = $pdo->query( $query );
			while($row = $rows->fetch(PDO::FETCH_ASSOC))
			{
				$MerchItem = array();
				
				$MerchItem['CL'] = $row['line_number'];
				$MerchItem['ID'] = $row['price_list_id'];
				$MerchItem['Name'] = $row['item_name'];
				$MerchItem['Qty'] = $row['qty'];
				$MerchItem['Price'] = $row['item_amount'];
				$MerchItem['Total'] = $row['total'];

				$results['Merchandise'][] = array('MerchItem'=>$MerchItem);
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "NOT_OK";
			$results['reason'] = "".$e;
		}
		
		return $results;
	}
	
	function getCaseCashAdvances( $p, $caseNum )
	{
		$query = "Select * From case_merchandise Where FDMS_case_num ='$caseNum'";
		$results = array();
		$pdo_str = "mysql:";
		$pdo_str .= "host=".$p['host'].";";
		$pdo_str .= "dbname=".$p['dbname'];
		
		try
		{
			$results['status'] = "OK";
			$results['rows'] = array();
			
			$items = array();
			
			$pdo = new PDO($pdo_str, $p['username'], $p['password']);
			$rows = $pdo->query( $query );
			while($row = $rows->fetch(PDO::FETCH_ASSOC))
			{
				$CashAdvance = array();
				
				$CashAdvance['CL'] = $row['line_number'];
				$CashAdvance['ID'] = $row['price_list_id'];
				$CashAdvance['Name'] = $row['item_name'];
				$CashAdvance['Qty'] = $row['qty'];
				$CashAdvance['Price'] = $row['item_amount'];

				$results['CashAdvances'][] = array('CashAdvance'=>$CashAdvance);
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "NOT_OK";
			$results['reason'] = "".$e;
		}
		
		return $results;
	}
	
	function getCaseServices( $p, $caseNum )
	{
		$query = "Select * From case_merchandise Where FDMS_case_num ='$caseNum'";
		$results = array();
		$pdo_str = "mysql:";
		$pdo_str .= "host=".$p['host'].";";
		$pdo_str .= "dbname=".$p['dbname'];
		
		try
		{
			$results['status'] = "OK";
			$results['rows'] = array();
			
			$items = array();
			
			$pdo = new PDO($pdo_str, $p['username'], $p['password']);
			$rows = $pdo->query( $query );
			while($row = $rows->fetch(PDO::FETCH_ASSOC))
			{
				$CaseService = array();
								
				$CaseService['CL'] = $row['line_number'];
				$CaseService['ID'] = $row['price_list_id'];
				$CaseService['Name'] = $row['item_description'];
				$CaseService['Qty'] = $row['qty'];
				$CaseService['Price'] = $row['item_amount'];

				$results['CaseServices'][] = array('CaseService'=>$CaseService);
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "NOT_OK";
			$results['reason'] = "".$e;
		}
		
		return $results;
	}

	function getFinishedCasesCaseNumbers($p)
	{
		$query = "Select FDMS_case_num,FDMS_vitals_id From cases Where status ='FINISHED'";
		$results = array();
		$pdo_str = "mysql:";
		$pdo_str .= "host=".$p['host'].";";
		$pdo_str .= "dbname=".$p['dbname'];
		
		try
		{
			$results['status'] = "OK";
			$results['rows'] = array();
			
			$items = array();
			
			$pdo = new PDO($pdo_str, $p['username'], $p['password']);
			$rows = $pdo->query( $query );
			while($row = $rows->fetch(PDO::FETCH_ASSOC))
			{
				$results['rows'][] = $row['FDMS_case_num'];
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "NOT_OK";
			$results['reason'] = "".$e;
		}
		
		return $results;
	}
	
	function getCase( $p, $caseNumber )
	{
		$results = array();
		
		$query = "Select * From cases Where FDMS_case_num = '$caseNumber'";
		$results = array();
		$pdo_str = "mysql:";
		$pdo_str .= "host=".$p['host'].";";
		$pdo_str .= "dbname=".$p['dbname'];
		
		try
		{
			$results['status'] = "OK";
			$results['rows'] = array();
			
			$pdo = new PDO($pdo_str, $p['username'], $p['password']);
			$rows = $pdo->query( $query );
			
			$row = $rows->fetch(PDO::FETCH_ASSOC);
			
			$caseXML = "<Case>";			
			$caseXML .= "<Number>$caseNumber</Number>";			
			$caseXML .= "<VitalsId>".$row['FDMS_vitals_id']."</VitalsId>";
			$caseXML .= "<Branch>".$row['branch_num']."</Branch>";
			$caseXML .= "<Status>".$row['status']."</Status>";
			$caseXML .= "<Choice>".$row['choice']."</Choice>";
			$caseXML .= "<Tribute>".$row['tribute_chosen']."</Tribute>";
			$caseXML .= "<Package>".$row['status']."</Package>";
			$caseXML .= "<Casket>".$row['casket']."</Casket>";
			$caseXML .= "<Urn>".$row['urn']."</Urn>";
			$caseXML .= "<Vault>".$row['vault']."</Vault>";
			
			$caseXML .= "<Merchandise>";
			$r = getCaseMerchandise( $p, $caseNumber );
			if($r['status']=="OK")
			{
				foreach( $r['Merchandise'] as $merchItemTag=>$merchItemNoodge )
				{
					$merchItem = $merchItemNoodge["MerchItem"];
					$caseXML .= "<MerchItem>";
					$caseXML .= "<CL>{$merchItem['CL']}</CL>";
					$caseXML .= "<ID>{$merchItem['ID']}</ID>";
					$caseXML .= "<Name>{$merchItem['Name']}</Name>";
					$caseXML .= "<Qty>{$merchItem['Qty']}</Qty>";
					$caseXML .= "<Price>{$merchItem['Price']}</Price>";
					$caseXML .= "<Total>{$merchItem['Total']}</Total>";
					$caseXML .= "</MerchItem>";
				}
			}
			$caseXML .= "</Merchandise>";
			
			$caseXML .= "<Services>";
			$r = getCaseServices( $p, $caseNumber );
			if($r['status']=="OK")
			{
				foreach( $r['CaseServices'] as $serviceTag=>$serviceNoodge )
				{
					$service = $serviceNoodge["CaseService"];
					$caseXML .= "<Service>";
					$caseXML .= "<CL>{$service['CL']}</CL>";
					$caseXML .= "<ID>{$service['ID']}</ID>";
					$caseXML .= "<Name>{$service['Name']}</Name>";
					$caseXML .= "<Qty>{$service['Qty']}</Qty>";
					$caseXML .= "<Price>{$service['Price']}</Price>";
					$caseXML .= "</Service>";
				}
			}
			$caseXML .= "</Services>";
			
			$caseXML .= "<CashAdvances>";
			$r = getCaseCashAdvances( $p, $caseNumber );
			if($r['status']=="OK")
			{
				foreach( $r['CashAdvances'] as $cashAdvanceTag=>$cashAdvanceNoodge )
				{
					$cashAdvance = $cashAdvanceNoodge["CashAdvance"];
					$caseXML .= "<CashAdvance>";
					$caseXML .= "<CL>{$cashAdvance['CL']}</CL>";
					$caseXML .= "<ID>{$cashAdvance['ID']}</ID>";
					$caseXML .= "<Name>{$cashAdvance['Name']}</Name>";
					$caseXML .= "<Qty>{$cashAdvance['Qty']}</Qty>";
					$caseXML .= "<Price>{$cashAdvance['Price']}</Price>";
					$caseXML .= "</CashAdvance>";
				}
			}
			$caseXML .= "</CashAdvances>";
			
			$caterer = $row['caterer'];
			$menu = $row['menu'];
			$headcount = $row['headcount'];
			$location = $row['location'];
			$datetime = $row['datetime'];
			$catering_total = $row['catering_total'];
			
			$caseXML .= "<Catering>";
			$caseXML .= "<Caterer>$caterer</Caterer>";
			$caseXML .= "<Menu>$menu</Menu>";
			$caseXML .= "<Headcount>$headcount</Headcount>";
			$caseXML .= "<Location>$location</Location>";
			$caseXML .= "<Total>$catering_total</Total>";
			$caseXML .= "</Catering>";
			
			$florist = $row['florist'];
			$arrangement = $row['arrangement'];
			$floral_total = $row['floral_total'];
			
			$caseXML .= "<Floral>";
			$caseXML .= "<Florist>$florist</Florist>";
			$caseXML .= "<Arrangements>$arrangement</Arrangements>";
			$caseXML .= "<Total>$total</Total>";
			$caseXML .= "</Floral>";
			
			$caseXML .= "</Case>";
			
			// construct the array to return.			
			$results['Case'] = $caseXML;
		}
		catch(Exception $e)
		{
			$results['status'] = "NOT_OK";
			$results['reason'] = "".$e;
		}
		
		return $results;
	}
	
	function getCases( $p, $caseNumbers )
	{
		$results = array();
		$results['cases'] = array();
		$results['status'] = "OK";
		foreach( $caseNumbers as $caseNumber )
		{
			$r = getCase( $p, $caseNumber );
			if($r['status']=="OK")
				$results['cases'][] = $r['Case'];
			else
			{
				$results['status'] = $r['status'];
				return $results;
			}
		}
		
		return $results;
	}
			
	// below here go the functions that support the web service.
	function getFinishedCaseNumbers($password)
	{
		if($password != "10key10")
			return "<result><status>NOT_OK_PASSWD</status></result>";
			
		$p = getConfigDbParams();
		$r = getFinishedCasesCaseNumbers($p);
		
		$resultXML = "<result>";
		$resultXML .= "<status>".$r['status']."</status>";
		$resultXML .= "<caseNums>";
		if(null != $r['rows'])
		{
			foreach( $r['rows'] as $row )
				$resultXML .= "<caseNum>".$row."</caseNum>";
		}
		$resultXML .= "</caseNums>";
		$resultXML .= "</result>";
		
		return $resultXML;
	}
	
	function getCaseDetailsForFinishedCase( $password, $caseNumbers )
	{
		if($password != "10key10")
			return array("status"=>"NOT_OK", "reason"=>"Invalid password");
		$p = getConfigDbParams();
		$r = getCases($p, array($caseNumbers));
		
		$resultXML = "<result>";
		$resultXML .= "<status>".$r['status']."</status>";
		$resultXML .= "<Cases>";
		if($r['status']=="OK")
		{
			var_dump($r);
			foreach( $r['cases'] as $case )
				$resultXML .= $case;
		}
		$resultXML .= "</Cases>";
		$resultXML .= "</result>";
		return $resultXML;
	}
		
	ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
	$server = new SoapServer("VAScases.wsdl");
	$server->addFunction("getFinishedCaseNumbers");
	$server->addFunction("getCaseDetailsForFinishedCase");	
	$server->handle();
?>
